package com.he.strategymodel.context;

import com.he.strategymodel.strategy.concretestrategy.SaleStategyDiscount;
import com.he.strategymodel.strategy.concretestrategy.SaleStategyNormal;
import com.he.strategymodel.strategy.concretestrategy.SaleStategyReturn;
import com.he.strategymodel.strategy.StategyInterface;

/**
 * 打折策略抽象类,,于工厂模式合用<br/>
 * 通过策略接口（StategyInterface），封装不同的算法（业务），在使用时通过相同的接口调用不同的算法，有助于算法的分离（解耦）<br/>
 * StategyContext则是调用策略接口的对象，于工厂方法合用，将策略接口的实现交给context，最大化减少客户端创建对象的职责以及对后台方法的了解
 */
public class StategyContext {


    StategyInterface<Double> stategy;

    public <T extends  StategyInterface> StategyContext(Class<T> clazz) {
        if (clazz == SaleStategyDiscount.class) {
            this.stategy = new SaleStategyDiscount();
        } else if (clazz == SaleStategyNormal.class) {
            this.stategy = new SaleStategyNormal();
        } else if (clazz == SaleStategyReturn.class) {
            this.stategy = new SaleStategyReturn();
        }
        throw new AssertionError("请传入合法打折算法！");
    }

    public double getFinalMoney(double originalMoney) {
        return stategy.stategy(originalMoney);
    }
}
